package it.nard.conf;

import it.nard.controller.AllevatoriController;
import it.nard.controller.LatteInEntrataController;
import it.nard.controller.LatteInGiacenzaController;
import it.nard.controller.LatteInUscitaController;
import it.nard.controller.RMozzarellaController;
import it.nard.controller.RProvolaController;
import it.nard.controller.RRicottaController;
import it.nard.utils.CopyFile;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchEvent.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.jfree.io.FileUtilities;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class JasperReportManager {

	
	public static final String ALLEVATORI_REPORT = AllevatoriController.ALLEVATORI_CODE+".jrxml";
	public static final String LATTE_ENTRATA_REPORT = LatteInEntrataController.LATTE_ENTRATA+".jrxml";
	public static final String LATTE_GIACENZA_REPORT = LatteInGiacenzaController.LATTE_GIACENZA+".jrxml";
	public static final String LATTE_USCITA_REPORT = LatteInUscitaController.LATTE_USCITA+".jrxml";
	public static final String RMOZZARELLA_REPORT = RMozzarellaController.R_MOZZARELLA+".jrxml";
	public static final String RPROVOLA_REPORT = RProvolaController.R_PROVOLA+".jrxml";
	public static final String RRICOTTA_REPORT = RRicottaController.R_RICOTTA+".jrxml";

//	private static final String BASE_REPORT_DIR = new File(".").getAbsolutePath()+"/src/main/resources/reports/";
	
	private static final String BASE_REPORT_DIR = "/reports/";
	
	
	private HashMap<String, JasperReport> jasperReportHm =  new HashMap<String, JasperReport>();
	
	private Properties properties;
	
	
	
	
	public JasperReportManager(){
	    	
		properties = new Properties();
		try {
			properties.load(this.getClass().getResourceAsStream("/properties.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String logoPath = properties.getProperty("tmpFolder")+"Logo.bmp";
		CopyFile.copy(this.getClass().getResourceAsStream("/Logo.bmp"), new File(logoPath));
		ArrayList<String> reportList = new ArrayList<String>();
		reportList.add(ALLEVATORI_REPORT);
		reportList.add(LATTE_ENTRATA_REPORT);
		reportList.add(LATTE_GIACENZA_REPORT);
		reportList.add(LATTE_USCITA_REPORT);
		reportList.add(RMOZZARELLA_REPORT);
		reportList.add(RPROVOLA_REPORT);
		reportList.add(RRICOTTA_REPORT);
		
		Iterator<String> iter = reportList.iterator();
		while(iter.hasNext()){
			String elem = iter.next();
			StringBuffer sb = new StringBuffer();
			
			try {
			
//				FileInputStream fstream = new FileInputStream(BASE_REPORT_DIR+elem);
		
				
//				FileInputStream fstream = new FileInputStream(this.getClass().getResourceAsStream(BASE_REPORT_DIR+elem).);
				
				
				
	            DataInputStream in = new DataInputStream(this.getClass().getResourceAsStream(BASE_REPORT_DIR+elem));
	            BufferedReader br = new BufferedReader(new InputStreamReader(in));
	            String strLine;
	            while ((strLine = br.readLine()) != null){
//	                System.out.println (strLine);
	            	strLine = strLine.replace("<$LOGO$>", logoPath);
	                if(strLine.indexOf("uuid=\"")>0){
	                	strLine = strLine.replace(strLine.substring(strLine.indexOf("uuid=\""), strLine.indexOf("uuid=\"")+43), "");
	                }
	                sb.append(strLine+"\n");
	           
	            }
	            in.close();
	         
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.exit(1);
			}
				
			 try {
				 
				 
			      FileOutputStream file = new FileOutputStream( properties.getProperty("tmpFolder")+elem);
			      PrintStream output = new PrintStream(file);
			      output.print(sb.toString());
			      output.close();
	
		    } catch (IOException e) {
		      System.out.println("Errore: " + e);
		      System.exit(1);
		    }
			 
			 
			try {
			InputStream inputStream = new FileInputStream (properties.getProperty("tmpFolder")+elem);
			JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
			JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
			jasperReportHm.put(elem, jasperReport);
			}catch(Exception e ){
				e.printStackTrace();
				System.exit(1);
			}
		}
	}
	
	
	public void generateReport (List<? extends Object> dataBeanList, String reportCode, String fileName, boolean isPdf){
				
		try{
				
				JRBeanCollectionDataSource beanColDataSource = new
				JRBeanCollectionDataSource(dataBeanList);
				 
				Map parameters = new HashMap();
				 
				
				JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReportHm.get(reportCode+".jrxml"), parameters, beanColDataSource);
				if(isPdf){
					JasperExportManager.exportReportToPdfFile(jasperPrint, fileName);
				}
				else{
					JasperExportManager.exportReportToHtmlFile(jasperPrint, fileName);
				}
				
			
		}catch(Exception e ){
			e.printStackTrace();
		}
		
	}
	
	
	

	
	            
}

